package com.loop.netty.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 1. 实例要求：Netty服务器在6668端口监听，浏览器发出请求“<a href="http://127.0.0.1:8081/">...</a>”
 *  ServerBootStrap在bind端口之后，可能收不到浏览器请求，可能是由于某些端口(6665-6669等)是浏览器的默认非安全端口，浏览器给拦截了
 * 2. 服务器可以回复消息给客户端：“hello 我是服务器”，并对特定请求资源进行过滤
 * 3. 目的：Netty可以做Http服务开发，并且理解Handler实例和客户端及其请求的关系
 * @author zhangxiaolu
 * @since 2023-09-12 22:14
 */
public class HttpNettyServer {

    public static void main(String[] args) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HttpServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(8081).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
